# -*- coding:utf-8 -*-
import gc

__author__ = 'root'
import codecs

import pandas as pd
from pandas import DataFrame
import os
from common_module.source_handles.kaoala_source_datafeici import source_seach_char
from common_module.path_handle import current_source_path
import time
import weakref


class dataframe_init:
    def __init__(self,word_dict_path=None):
        self.__word_dict_path=word_dict_path   #分词词汇文件
        self.__word_dict_pd_name=os.path.join(current_source_path,'word_dict.csv')   #分词表
        self.__proportion_file_path=os.path.join(current_source_path,'word_word_proportion.csv')   #分词与分词之间的比重关系
        self.__word_material_proportion_file_path=os.path.join(current_source_path,'word_material_proportion.csv')   #词库与素材权重映射
        self.__proportion_material_file_path=['weights_1.csv','weights_2.csv','weights_3.csv','weights_4.csv','weights_5.csv','weights_6.csv']   #权重与素材映射表名字
        self.__material_solidify_file_path=os.path.join(current_source_path,'material_solidify.csv')   #s素材固化表

        self.__word_columns=['word']
        self.__proportion_columns=['word_id','proportion_1','proportion_2','proportion_3','proportion_4','proportion_5','proportion_6']
        self.__word_material_proportion_colunms=['word_id','weights_1','weights_2','weights_3','weights_4','weights_5','weights_6']   #词库与素材权重映射的字段名字
        self.__proportion_material_colunms=['father_id','material_id','p_category']     #权重与素材映射表的字段名字
        self.__material_solidify_colunms=['qtid','qcid','hid','atid','acid','mid','inid','pic']


        self.word_dataframe=None
        self.proportion_dataframe=None
        self.word_material_proportion_dataframe=None
        self.proportion_material_6_dataframe=None    #weights_6.csv表的数据
        self.material_solidify_dataframe=None


    def load_ram(self):
        '''
        把所有的表加载到内存
        :return:
        '''
        words=[]   #分词词汇
        with codecs.open(self.__word_dict_path,mode='r',encoding='utf-8') as f:
            for line in f.readlines():
                line_list=line.split()
                if line_list.__len__()<3:
                    continue
                words.append(line_list[0])
        self.word_dataframe=DataFrame(data={'word':words},columns=self.__word_columns)
        self.word_dataframe.to_csv(self.__word_dict_pd_name,encoding='utf-8',index=False)

        word_index_list=self.word_dataframe.index.values
        proportion_data={'word_id':word_index_list,'proportion_6':[str(i) for i in word_index_list]}
        self.proportion_dataframe=DataFrame(data=proportion_data,columns=self.__proportion_columns)
        self.proportion_dataframe.to_csv(self.__proportion_file_path,encoding='utf-8',index=False)


        word_material_proportion_data={'word_id':word_index_list,'weights_1':['weights_1.csv' for i in word_index_list],'weights_2':['weights_2.csv' for i in word_index_list],'weights_3':['weights_3.csv' for i in word_index_list],'weights_4':['weights_4.csv' for i in word_index_list],
              'weights_5':['weights_5.csv' for i in word_index_list],'weights_6':['weights_6.csv' for i in word_index_list]}

        self.word_material_proportion_dataframe=DataFrame(data=word_material_proportion_data,columns=self.__word_material_proportion_colunms)
        self.word_material_proportion_dataframe.to_csv(self.__word_material_proportion_file_path,encoding='utf-8',index=False)

        if os.path.isfile(os.path.join(current_source_path,self.__proportion_material_file_path[-1]))==False:   #如果权重与素材映射表不存在
            self.proportion_material_6_dataframe=DataFrame(data={},columns=self.__proportion_material_colunms)
            print 'self.proportion_material_6_dataframe--->chengg'
        else:
            self.proportion_material_6_dataframe=pd.read_csv(os.path.join(current_source_path,self.__proportion_material_file_path[-1]),encoding='utf-8')

        if os.path.isfile(self.__material_solidify_file_path)==False:
            self.material_solidify_dataframe=DataFrame(data={},columns=self.__material_solidify_colunms)
            print 'self.material_solidify_dataframe--->chengg'
        else:
            self.material_solidify_dataframe=pd.read_csv(self.__material_solidify_file_path,encoding='utf-8')

    def release_ram(self):
        del self.word_dataframe
        del self.proportion_dataframe
        del self.word_material_proportion_dataframe
        del self.proportion_material_6_dataframe    #weights_6.csv表的数据
        del self.material_solidify_dataframe
        gc.collect()



class proportion_material_table:
    def __init__(self,word_dataframe,word_material_proportion_dataframe,proportion_material_6_dataframe):
        self.word_dataframe=word_dataframe   #分词词汇的dataframe
        self.word_material_proportion_dataframe=word_material_proportion_dataframe   #词库与素材权重映射的databfram
        self.proportion_material_6_dataframe=proportion_material_6_dataframe  #权重与素材映射 100比重的dataframe
        self.__proportion_material_file_path=['weights_1.csv','weights_2.csv','weights_3.csv','weights_4.csv','weights_5.csv','weights_6.csv']   #权重与素材映射表名字
        self.word_dataframe_dict={}
        self.word_material_proportion_dataframe_dict={}
        self.proportion_material_6_dataframe_dict={}


    def dataframe_to_dcit(self):
        '''
        把pandas的dataframe数据格式转成dict
        :return:
        '''
        self.word_dataframe_dict=self.word_dataframe.to_dict()
        self.word_material_proportion_dataframe_dict=self.word_material_proportion_dataframe.to_dict()
        self.proportion_material_6_dataframe_dict=self.proportion_material_6_dataframe.to_dict()




    def write_proportion_material(self,word_list=[],p_category=None,material_id=None):
        '''
        对权重与素材映射的100比重的表进行写入
        :param word_list:分词的列表 type list
        :param p_category: 素材类型 type int  1文本题目 2图片题目 3提示题目4 文本答案 5图片答案 6视频考点 7互动考点
        :param material_id: 素材id type int
        :return:
        '''
        filter_word_dataframe=self.word_dataframe[self.word_dataframe['word'].isin(word_list)]  #过滤出来的分词 dataframe
        filter_word_dataframe_index=filter_word_dataframe.index.values
        del filter_word_dataframe

        word_material_proportion_dataframe=self.word_material_proportion_dataframe[self.word_material_proportion_dataframe['word_id'].isin(filter_word_dataframe_index)]

        word_material_dataframe_index=word_material_proportion_dataframe.index.values   #词库与素材权重映射的index
        del word_material_proportion_dataframe

        #for sub_index in word_material_dataframe_index:
        weights_6_filter=self.proportion_material_6_dataframe[(self.proportion_material_6_dataframe['father_id'].isin(word_material_dataframe_index))&(self.proportion_material_6_dataframe['material_id'].isin([material_id]))&(self.proportion_material_6_dataframe['p_category'].isin([p_category]))]   #过滤出素材对应的记录是否存在
        father_id_list=set(map(lambda  x:x.get('father_id',None), weights_6_filter.to_dict().values()))  #已经存在的记录id
        for sub_father in word_material_dataframe_index:
            if sub_father not in father_id_list:  #判断词组id是否为None
                self.proportion_material_6_dataframe.loc[len(self.proportion_material_6_dataframe)]=[sub_father,material_id,p_category]


        del weights_6_filter
        time.sleep(0.1)

    # def write_proportion_material(self,word_list=[],p_category=None,material_id=None):
    #     filter_word_list=[]
    #     for key,value in self.word_dataframe_dict.items():
    #         word=value.get('word','')
    #         if word!='' and word in word_list:
    #             filter_word_list.append(key)
    #     word_material_dataframe_list=[]
    #     for key,value in self.word_material_proportion_dataframe_dict.items():
    #         word_id=value.get('word_id',None)
    #         if word_id!=None and word_id in filter_word_list:
    #             word_material_dataframe_list.append(key)




    def solidify(self):
        '''
        把权重与素材映射表固化到csv
        :return:
        '''
        self.proportion_material_6_dataframe.to_csv(os.path.join(current_source_path,self.__proportion_material_file_path[-1]),encoding='utf-8',index=False)


    def release_ram(self):
        del self.word_dataframe
        del self.word_material_proportion_dataframe
        del self.proportion_material_6_dataframe    #weights_6.csv表的数据
        gc.collect()



class material_solidify_table:
    def __init__(self,material_solidify_dataframe):
        self.__material_solidify_file_path=os.path.join(current_source_path,'material_solidify.csv')   #s素材固化表
        self.material_solidify_dataframe=material_solidify_dataframe
        self.__material_solidify_colunms=['qtid','qcid','hid','atid','acid','mid','inid','pic']


    def write_material_solidify(self,material_id=None,p_category=None,pic_meida_path=''):
        '''
        所素材进行固化
        :param material_id: 素材id type int
        :param p_category:  素材类型 type int 1文本题目 2图片题目 3提示题目4 文本答案 5图片答案 6视频考点 7互动考点
        :param pic_meida_path: 图片或视频的路径
        :return:
        '''
        items={}
        items['code']=1  #0是成功 1为失败
        items['msg']=u''
        if p_category not in [1,2,3,4,5,6,7]:
            items['msg']=u'素材的类型错误'
            return items
        if isinstance(material_id,int)==False:
            items['msg']=u'素材的id错误'
            return items
        solidify_filter=self.material_solidify_dataframe[self.material_solidify_dataframe[self.__material_solidify_colunms[p_category-1]].isin([material_id])]
        solidify_index=solidify_filter.index.values
        if solidify_index.__len__()<1:
            one_data=['','','','','','','',pic_meida_path]
            one_data[p_category-1]=material_id
            self.material_solidify_dataframe.loc[len(self.material_solidify_dataframe)]=one_data
        items['code']=0
        items['msg']=u'素材固化表增加成功'
        return items

    def solidify(self):
        '''
        素材固化到csv
        :return:
        '''
        self.material_solidify_dataframe.to_csv(self.__material_solidify_file_path,encoding='utf-8',index=False)





